package cn.lxh.ymcc.web.controller;

import cn.lxh.ymcc.dto.CourseAddDto;
import cn.lxh.ymcc.service.ICourseService;
import cn.lxh.ymcc.domain.Course;
import cn.lxh.ymcc.query.CourseQuery;
import cn.lxh.ymcc.result.JSONResult;
import cn.lxh.ymcc.result.PageList;
import cn.lxh.ymcc.service.ICourseTypeService;
import cn.lxh.ymcc.vo.CourseDetailInfoVo;
import cn.lxh.ymcc.vo.CourserOrderVo;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ICourseTypeService courseTypeService;

    /**
     * 保存和修改公用的
     */
//    @RequestMapping(value="/save",method= RequestMethod.POST)
//    public JSONResult saveOrUpdate(@RequestBody Course course){
//        if(course.getId()!=null){
//            courseService.updateById(course);
//        }else{
//            courseService.insert(course);
//        }
//        return JSONResult.success();
//    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        page = courseService.selectPage(page);
        return JSONResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }

    /**
     * 保存 一张大表。
     * 分别保存到 各个表中
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveBigTable(@RequestBody @Valid CourseAddDto dto) {
        courseService.saveBigTable(dto);
        return JSONResult.success();
    }


    /**
     * 课程上线
     * onLineCourse/1   课程上线
     */
    @RequestMapping(value = "/onLineCourse/{courseId}", method = RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId) {
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }

//    offLineCourse

    /**
     * 课程上线
     * onLineCourse/1   课程上线
     */
    @RequestMapping(value = "/offLineCourse/{courseId}", method = RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("courseId") Long courseId) {
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     * 查询课程详情
     * @param courseId 课程Id
     * @return  课程详情页需要的数据
     */
    @RequestMapping(value="/detail/data/{courseId}",method= RequestMethod.GET)
    public JSONResult searchCourseDetail(@PathVariable("courseId") Long courseId){
        CourseDetailInfoVo vo = courseService.searchCourseDetail(courseId);
        return JSONResult.success(vo);
    }


    /**
     * 订单详情的展示--
     *      数据结构的设计 dto  一个 vo
     *  前端要什么
     *  最大的coureseOderVo
     *      -courseInfos<courseDto> --list
     *          -courseDto
     *              -course
     *              -courseMarket
     *      -totalAmount
     * 后面要下单需要这个CourserOrderVo对象 于是放到了pojo-course中
     *
     *
     * @param courseIds 课程Id
     * @return  课程详情页需要的数据
     */
    @RequestMapping(value="/info/{courseIds}",method= RequestMethod.GET)
    public JSONResult searchCourseOrder(@PathVariable("courseIds") String courseIds){
        //数据结构的设计
        CourserOrderVo vo = courseService.searchCourseOrder(courseIds);
        return JSONResult.success(vo);
    }

}


